package com.xiaodu.jdbc.config;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.spring.stat.BeanTypeAutoProxyCreator;
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.xiaodu.jdbc.controller.UserController;
import org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author: dutianjie
 * @date: 2021/4/12
 * @version: v1.0.0
 * @description: 描述
 **/
@Configuration
@ConditionalOnClass(value = {DataSource.class})
public class DatasourceConfig {

    @Configuration
    @ConditionalOnClass(value = {DruidDataSource.class})
    public static class DruidDatasourceConfig {

        /*-------------使用配置类配置druid相关信息-----------------*/

//        @Bean
//        public DruidDataSource dataSource(DataSourceProperties dataSourceProperties) throws SQLException {
//            DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
//            dataSource.setFilters("slf4j,wall,stat");
//            dataSource.setMaxActive(5);
//            return dataSource;
//        }
//
//        @Bean
//        public WallFilter wallFilter() {
//            WallFilter wallFilter = new WallFilter();
//            wallFilter.setConfig(wallConfig());
//            wallFilter.setThrowException(true); // 对被认为是攻击的sql进行抛异常
//            wallFilter.setLogViolation(true); // 对被认为是攻击的sql进行error 输出
//            return wallFilter;
//        }
//
//        @Bean
//        public WallConfig wallConfig() {
//            WallConfig wallConfig = new WallConfig();
//            wallConfig.setDeleteWhereAlwayTrueCheck(true);
//            wallConfig.setUpdateWhereAlayTrueCheck(true);
//            wallConfig.setDeleteWhereNoneCheck(true);
//            wallConfig.setUpdateWhereNoneCheck(true);
//            return wallConfig;
//        }
//
//        @Bean
//        public StatFilter statFilter() {
//            StatFilter statFilter = new StatFilter();
//            statFilter.setDbType("mysql");
//            statFilter.setLogSlowSql(true);
//            statFilter.setSlowSqlMillis(2000);
//            return statFilter;
//        }
/*--------配置spring 监控--------------*/
        @Bean
        public DruidStatInterceptor druidStatInterceptor() throws Exception {
            DruidStatInterceptor druidStatInterceptor = new DruidStatInterceptor();
            druidStatInterceptor.afterPropertiesSet();
            return druidStatInterceptor;
        }

        @Bean
        public BeanTypeAutoProxyCreator beanTypeAutoProxyCreator() {
            BeanTypeAutoProxyCreator beanTypeAutoProxyCreator = new BeanTypeAutoProxyCreator();
            beanTypeAutoProxyCreator.setTargetBeanType(UserController.class);  // 拦截的目标
            beanTypeAutoProxyCreator.setInterceptorNames("druidStatInterceptor"); // bean拦截器名称
            return beanTypeAutoProxyCreator;
        }
        /**使用正则方法名匹配，通过aop**/
        @Bean
        public JdkRegexpMethodPointcut jdkRegexpMethodPointcut() {
            JdkRegexpMethodPointcut jdkRegexpMethodPointcut = new JdkRegexpMethodPointcut();
            jdkRegexpMethodPointcut.setPatterns("com.xiaodu.jdbc.controller.UserController.*"); // 包扫描
            return jdkRegexpMethodPointcut;
        }

        @Bean
        public Advisor druidAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut jdkRegexpMethodPointcut) {
            Advisor advisor = new DefaultPointcutAdvisor(jdkRegexpMethodPointcut, druidStatInterceptor);
            return advisor;
        }

    }

}
